using System;
using System.Collections.Generic;
using TXLooker.Common;
using TXLooker.Common.Core;

namespace TXLooker.WorkItemExtention.Core
{
    internal class WIObjectsStore
    {
        public static void AddServers( IEnumerable<WIServerProvider> servers )
        {
            List<WIServerProvider> items = new List<WIServerProvider>();

            lock( m_serversMAP )
            {
                foreach( WIServerProvider item in servers )
                {
                    if( m_serversMAP.ContainsKey( item.ServerUri ) )
                        continue;

                    m_serversMAP.Add(item.ServerUri, item);
                    items.Add(item);
                }
            }

            RaiseNewServers( items );
        }
        public static void RemoveServers( IEnumerable<Uri> uries )
        {
            lock( m_serversMAP )
            {
                List<WIServerProvider> items = new List<WIServerProvider>();

                foreach( Uri uri in uries )
                {
                    if( !m_serversMAP.ContainsKey( uri ) )
                        continue;

                    items.Add( m_serversMAP[uri] );
                }

                if( items.Count != 0 )
                    RaisePreRemoveServers( items );
            }
        }
        public static IEnumerable<WIServerProvider> Servers
        {
            get
            {
                lock( m_serversMAP )
                {
                    foreach( WIServerProvider item in m_serversMAP.Values )
                        yield return item;
                }
            }
        }
        private static Dictionary<Uri, WIServerProvider> m_serversMAP = new Dictionary<Uri, WIServerProvider>();

        #region event NewServers

        public static event TFEventHandler<IEnumerable<WIServerProvider>> NewServers
        {
            add    { lock( m_syncNewServers ) { m_OnNewServersEH += value; } }
            remove { lock( m_syncNewServers ) { m_OnNewServersEH -= value; } }
        }
        private static TFEventHandler<IEnumerable<WIServerProvider>> m_OnNewServersEH = null;
        private static readonly object m_syncNewServers = new object();

        private static void RaiseNewServers( IEnumerable<WIServerProvider> servers )
        {
            CommonGlobal.RaiseEventHandler( m_OnNewServersEH, new object[] { servers }, m_syncNewServers );
        }

        #endregion event NewServers

        #region event DeleteServers

        public static event TFEventHandler<IEnumerable<WIServerProvider>> PreRemoveServers
        {
            add    { lock( m_syncPreRemoveServers ) { m_OnPreRemoveServersEH += value; } }
            remove { lock( m_syncPreRemoveServers ) { m_OnPreRemoveServersEH -= value; } }
        }
        private static TFEventHandler<IEnumerable<WIServerProvider>> m_OnPreRemoveServersEH = null;
        private static readonly object m_syncPreRemoveServers = new object();

        private static void RaisePreRemoveServers(IEnumerable<WIServerProvider> servers)
        {
            CommonGlobal.RaiseEventHandler( m_OnPreRemoveServersEH, new object[] { servers }, m_syncPreRemoveServers );
        }

        #endregion event DeleteServers


        public static WIServerProvider GetServer( string uri )
        {
            return GetServer( new Uri(uri) );
        }
        public static WIServerProvider GetServer( Uri uri )
        {
            lock( m_serversMAP )
            {
                if( m_serversMAP.ContainsKey(uri) )
                    return m_serversMAP[uri];
            }

            return null;
        }
    }
}
